//
//  UIViewExtension.swift
//  MGExtensionsKit
//
//  Created by Max on 2019/8/20.
//

import Foundation

public extension UIView {
    var x: CGFloat {
        set {
            var f = frame
            f.origin.x = newValue
            frame = f
        }
        get {
            return frame.origin.x
        }
    }
    
    var y: CGFloat {
        set {
            var f = frame
            f.origin.y = newValue
            frame = f
        }
        get {
            return frame.origin.y
        }
    }
    
    var centerX: CGFloat {
        set {
            var c = center
            c.x = newValue
            center = c
        }
        get {
            return center.x
        }
    }
    
    /// view width
    var width: CGFloat {
        set {
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
        get {
            return self.frame.width
        }
    }
    
    /// view height
    var height: CGFloat {
        set {
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
        get {
            return self.frame.height
        }
    }
    
    var bottom: CGFloat {
        return frame.maxY
    }
    
    var viewController: UIViewController? {
        var responder: UIResponder = self
        while responder.next != nil {
            responder = responder.next!
            if responder is UIViewController {
                return responder as? UIViewController
            }
        }
        return nil;
    }
}

/// 圆角相关
public extension UIView {
    /// 四周圆角
    func corRadius(radius: CGFloat, borderWidth: CGFloat = 0, borderColor: UIColor = UIColor.clear) {
        self.layer.cornerRadius = radius
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
        self.layer.masksToBounds = true
    }
    
    /// 部分圆角
    func corners(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
}

/// 颜色相关
public extension UIView {
    /// 设置阴影
    func shadow(at top: Bool) {
        layer.shadowColor = UIColor(hexString: "#B2B2B2")?.cgColor
        layer.shadowRadius = 1.5
        layer.shadowOffset = CGSize(width: 0, height: top ? -3 : 3)
        layer.shadowOpacity = 0.25
        layer.shadowPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [], cornerRadii: .zero).cgPath
    }
    
    /// 设置阴影
    func setShadow(color: UIColor, radius: CGFloat,
                   offset: CGSize, opacity: Float) {
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOffset = offset
        layer.shadowOpacity = opacity
    }
    
    /// 设置渐变色
    func setGradient(from color: UIColor, to toColor: UIColor, vertical: Bool = false) {
        if let gradientLayer = layer.sublayers?.first(where: { $0 is CAGradientLayer }) {
            gradientLayer.removeFromSuperlayer()
        }
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [color.cgColor, toColor.cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = vertical ? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0)
        gradientLayer.frame = bounds
        layer.insertSublayer(gradientLayer, at: 0)
    }
    
    func setGradient(colors: [UIColor],
                     locations: [NSNumber]? = nil,
                     from point1: CGPoint = CGPoint(x: 0, y: 0),
                     to point2: CGPoint = CGPoint(x: 0, y: 1),
                     index: Int? = nil) {
        layer.sublayers?.forEach({ ($0 as? CAGradientLayer)?.removeFromSuperlayer() })
        
        let colors = colors.map({ $0.cgColor })
         
        // 创建渐变图层并添加到视图上
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = colors
        if let locations = locations {        
            gradientLayer.locations = locations
        }
        gradientLayer.startPoint = point1 // 起始点位置
        gradientLayer.endPoint = point2   // 结束点位置
        gradientLayer.frame = bounds
        if let index = index {
            layer.insertSublayer(gradientLayer, at: UInt32(index))
        } else {
            layer.addSublayer(gradientLayer)
        }
    }
}
